package main.java.com.itlin.datastructure.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

public class DoublyLinkedListSentinel implements Iterable<Integer> {//整体

    //head 哨兵
    private Node head;//哨兵节点

    private Node tail;

    //节点类
    private static class Node {

        Node pre;//上一个节点指针
        int value; //值
        Node next;//下一个节点指针

        public Node(Node pre,int value ,Node next){
            this.pre = pre;
            this.value = value;
            this.next = next;
        }
    }

    public DoublyLinkedListSentinel(){
        head = new Node(null,666,null);
        tail = new Node(null, 888, null);
        head.next = tail;
        tail.pre = tail;
    }
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node pointer = head.next;
            @Override
            public boolean hasNext() {//询问是否有下一个元素
                return pointer != tail;
            }

            @Override
            public Integer next() {//返回当前元素的值，并且指向下一个元素
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }





    public void addFirst(int value) {
        insert(0,value);
    }

    public void loop1(Consumer<Integer> consumer){
        Node pointer =  head.next;
        while(pointer != null){
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }

    public void loop2(Consumer<Integer> consumer){
        for (Node pointer = head.next; pointer != null; pointer = pointer.next){
            consumer.accept(pointer.value);
        }
    }

    private Node findLast(){

        //链表非空的情况
        Node last = head;
        while(last.next != null){
            last = last.next;
        }
        return last;
    }

    public void addLast(int value){

        Node last = tail.pre;
        Node added = new Node(last, value, tail);
        last.next = added;
        tail.pre =  added;
    }

    public void removeLast(){

        Node remove = tail.pre;
        if (remove == head){
            throw illegalIndex(0);
        }
        Node previous = remove.pre;
        previous.next = tail;
        tail.pre = previous;
    }


    private Node findNode(int index){
        int i = -1;
        for (Node pointer = head; pointer != null; pointer = pointer.next,i++){
            if (i == index) {
                return pointer;
            }
        }
        return null;
    }

    public int get(int index) throws IllegalArgumentException{
        Node node = findNode(index);
        if(node == null){
            throw illegalIndex(index);
        }else {
            return node.value;
        }
    }

    private static IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index[%d] 不合法%n", index));
    }

    public void insert(int index,int value){


        //先查找插入点前节点和后节点
        Node previous = findNode(index - 1);
        if (previous == null) {
            throw illegalIndex(index);
        }
        Node next = previous.next;
        Node inserted = new Node(previous,value,next);
        previous.next = inserted;
        next.pre = inserted;


    }

    public void removeFirst(){
        remove(0);
    }

    public void remove(int index){


        Node previous = findNode(index - 1);
        if (previous == null){
            throw illegalIndex(index);
        }
        Node removed = previous.next;
        if (removed == tail){
            throw illegalIndex(index);
        }
        Node next = removed.next;
        previous.next = next;
        next.pre = previous;

    }



}


